package org.rayken.trix.commons.game;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.rayken.trix.commons.game.runTypes.RunFactory;

public class Game {
	
	private List<Player> players = new ArrayList<Player>();
	private Map<Player, Integer> scores = new HashMap<Player, Integer>(); 
	private Map<Player, List<Run>> runs = new HashMap<Player, List<Run>>(); 
	
	public Game(Player p1, Player p2, Player p3, Player p4) {
		players.add(p1);
		players.add(p2);
		players.add(p3);
		players.add(p4);
		
		scores.put(p1, 0);
		scores.put(p2, 0);
		scores.put(p3, 0);
		scores.put(p4, 0);
		
		runs.put(p1, new ArrayList<Run>());
		runs.put(p2, new ArrayList<Run>());
		runs.put(p3, new ArrayList<Run>());
		runs.put(p4, new ArrayList<Run>());
	}

	
	
	public Map<Player, Integer> getScores() {
		return scores;
	}

	public List<Player> getPlayers() {
		return players;
	}

	void addScore(Player player, int scoreToAdd){
		scores.put(player, scores.get(player) + scoreToAdd);
	}
	
	public Player selectRandomPlayer() {
		Random rand = new Random();
		return players.get(rand.nextInt(4));
	}

	public Run playRun(RunType runType, Player caller, boolean forced) {
		Run run = RunFactory.createRun(runType, this, caller, forced);
		runs.get(caller).add(run);
		return run;
	}
	
	public List<RunType> getAvailableRunType(Player player){
		List<RunType> runTypes = Arrays.asList(RunType.values());
		
		for(Run run : runs.get(player)){
			if (runTypes.contains(run.getType()))
				runTypes.remove(run.getType());
		}
		return runTypes;
	}

	public Player nextPlayer(Player player) {
		return players.get((players.indexOf(player) + 1) % 4);
	}

}
